package com.itheima.controller;

/**
 * @author Dugen.Zhu
 * @date 2019-12-29 11:16
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 检查项后台
 */
@Controller
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")//查找服务
    @ResponseBody
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            Result result = checkGroupService.add(checkGroup, checkitemIds);
            if (result.isFlag()) {
                return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
            } else {
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @param queryPageBean 封装的查询页面对象
     * @return
     */
    @RequestMapping("/findPage")
    @ResponseBody
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        /*private Integer currentPage;//页码
        private Integer pageSize;//每页记录数
        private String queryString;//查询条件*/
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (currentPage <= 0 || pageSize <= 0) {
            return new Result(false, "您输入的传递页码或页大小为非法参数！");
        }
        try {
            PageResult pageResult = checkGroupService.findPage(currentPage, pageSize, queryString);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer id) {
        try {
            checkGroupService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }


    /**
     * 查找单条检查组
     *
     * @param id 根据id查找单条检查组
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 编辑检查组
     *
     * @param checkGroup 获取并封装的编辑数据
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.edit(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查找所有检查组
     *
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll() {
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
